//
//  ShuaiXuanTableViewController.m
//  Car
//
//  Created by lanou on 15/9/23.
//  Copyright (c) 2015年 朱鸿儒. All rights reserved.
//

#import "ShuaiXuanTableViewController.h"
#import "ShuaiXuanTableViewCell.h"
@interface ShuaiXuanTableViewController ()
@property (nonatomic,strong)NSMutableArray *shuaiXuanArray;
@end

@implementation ShuaiXuanTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [ProgressHUD show:@"正在加载..."];
    [self setUpData];
    [self reloadAndJosnWith:kShuaiXuan];
}
- (NSMutableArray *)shuaiXuanArray{
    if (!_shuaiXuanArray) {
        _shuaiXuanArray = [[NSMutableArray alloc]init];
    }
    return _shuaiXuanArray;
}
- (void)reloadAndJosnWith:(NSString *)url{
    [LORequestManger GET:url success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        for (NSDictionary *Dic in dic[@"result"][@"seriesitems"]) {
            ShuaiXuanModel *model = [ShuaiXuanModel JosnWithDic:Dic];
            [self.shuaiXuanArray addObject:model];
        }
        [self.tableView reloadData];
        [ProgressHUD dismiss];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (void)setUpData{
    __block ShuaiXuanTableViewController *mark = self;
    __block int a = 1;
    [self.tableView addFooterWithCallback:^{
        a++;
        NSLog(@"%d",a);
        NSString *url = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.8.8/cars/searchcars-pm1-mip0-map0-l-c-b-st-dsc-conf-o2-p%d-s20-bid-f-driv-numseats.json",a];
        [mark reloadAndJosnWith:url];
        [mark.tableView footerEndRefreshing];
    }];
    
    
    UIBarButtonItem *aButton = [[UIBarButtonItem alloc]initWithTitle:@"品牌" style:(UIBarButtonItemStyleDone) target:self action:@selector(aButtonClick:)];
    UIBarButtonItem *bButton = [[UIBarButtonItem alloc]initWithTitle:@"筛选" style:(UIBarButtonItemStyleDone) target:self action:@selector(bButtonClick:)];
    UIBarButtonItem *cButton = [[UIBarButtonItem alloc]initWithTitle:@"降价" style:(UIBarButtonItemStyleDone) target:self action:@selector(cButtonClick:)];
    UIBarButtonItem *dButton = [[UIBarButtonItem alloc]initWithTitle:@"二手车" style:(UIBarButtonItemStyleDone) target:self action:@selector(dButtonClick:)];
    NSArray *buttonArray = [[NSArray alloc]initWithObjects:aButton,bButton,cButton,dButton, nil];
    self.navigationItem.leftBarButtonItems = buttonArray;
    [self.tableView registerClass:[ShuaiXuanTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
}

- (void)aButtonClick:(UIBarButtonItem *)button{
    FindTableViewController *findVC = [[FindTableViewController alloc]init];
    SaleTableViewController *saleVC = [[SaleTableViewController alloc]init];
    IIViewDeckController *deckVC = [[IIViewDeckController alloc]initWithCenterViewController:findVC leftViewController:saleVC];
    deckVC.leftSize = 75;
    [self.navigationController pushViewController:deckVC animated:NO];
    
    
}
- (void)bButtonClick:(UIBarButtonItem *)button{
    ScreenTableViewController *screenVC = [[ScreenTableViewController alloc]init];
    [self.navigationController pushViewController:screenVC animated:NO];
}
- (void)cButtonClick:(UIBarButtonItem *)button{
    MarkDownTableViewController *markDownVC = [[MarkDownTableViewController alloc]init];
    [self.navigationController pushViewController:markDownVC animated:NO];
}
- (void)dButtonClick:(UIBarButtonItem *)button{
    UsedCarTableViewController *usedCarVC = [[UsedCarTableViewController alloc]init];
    [self.navigationController pushViewController:usedCarVC animated:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.shuaiXuanArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ShuaiXuanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = 0;
    [cell setWithModel:self.shuaiXuanArray[indexPath.row]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75;
}
@end
